package com.zyk.grate_offer.class01;

import java.io.File;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author zhangsan
 * @date 2021/4/21 9:40
 */
public class Code02_NumberOfFiles {

    public static int numberOfFiles(String path) {
        File root = new File(path);
        if(!root.exists()) return 0;
        if(root.isFile()) return 1;

        Queue<File> q = new LinkedList<>();
        q.add(root);
        File cur;
        int count = 0;
        while (!q.isEmpty()) {
            cur = q.poll();
            for (File file : cur.listFiles()) {
                if(file.isFile()) {
                    count++;
                }else if(file.isDirectory()) {
                    q.add(file);
                }
            }
        }
        return count;
    }



    // for test
    public static void main(String[] args) {
        String path = "C:\\Users\\zhangsan\\Desktop\\";
        System.out.println(numberOfFiles(path));
    }
}
